package com.desire.common.Base;

import com.desire.bean.common.PageBean;
import com.desire.utils.MapUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * @author LittleDesire
 * @description
 * @date 2022-05-07 周六 15:09
 * @since 17
 */
public abstract class BaseServiceImpl<T, M extends BaseMapper> implements BaseService<T> {

    @Autowired(required = false)
    M mapper;

    @Override
    public int save(T mapOrEntity) {
        return mapper.save(mapOrEntity);
    }

    @Override
    public int update(T mapOrEntity) {
        return mapper.update(mapOrEntity);
    }

    @Override
    public int delete(Serializable id) {
        return mapper.delete(id);
    }

    @Override
    public T get(Serializable id) {
        return (T) mapper.get(id);
    }

    @Override
    public PageBean query(Map map) {
        PageHelper.startPage(MapUtils.getPageNumValue(map), MapUtils.getPageSizeValue(map));
        List list = mapper.query(map);
        Page page = (Page) list;
        return new PageBean(page.getPageNum(), page.getPageSize(), page.getTotal(), page.getPages(), list);
    }
}
